package com.sense.kukoo.biz_media.view

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.biz_media.R
import com.sense.kukoo.biz_media.adapter.CommentContentAdapter
import com.sense.kukoo.biz_media.databinding.BizMediaFragmentMediaCommentBinding
import com.sense.kukoo.biz_media.vm.MediaCommentViewModel
import com.sense.kukoo.common.mvvm.view.BaseMvvmFragment
import com.sense.kukoo.common.repo.http.bean.CommentRecordBean
import com.sense.kukoo.common.wiget.dialog.AddCommentDialog
import com.sense.kukoo.common.wiget.dialog.DeletePopupWindow
import com.sense.kukoo.common.wiget.state.NetworkErrorState
import com.sense.kukoo.lib_logger.LoggerManager
import com.zy.multistatepage.state.SuccessState

class MediaCommentFragment: BaseMvvmFragment<MediaCommentViewModel, BizMediaFragmentMediaCommentBinding>() {

    private val TAG: String = javaClass.simpleName

    private lateinit var mAdapter: CommentContentAdapter
    private var contentId: Long = -1
    private var creatorId: Long = -1
    private var currentUserId: Long = -1
    private var afterInit: Boolean = false
    private var afterLoad: Boolean = false

    private var mAddCommentDialog: AddCommentDialog? = null
    private var mDeletePopupWindow: DeletePopupWindow? = null

    override fun initParam() {}

    override fun responseVM(action: Int) {}

    override fun getLayout(): Int = R.layout.biz_media_fragment_media_comment

    override fun getViewModelClass(): Class<MediaCommentViewModel> = MediaCommentViewModel::class.java

    companion object {
        @JvmStatic
        fun newInstance() = MediaCommentFragment()
    }

    fun setData(contentId: Long, creatorId: Long, currentUserId: Long){
        this.contentId = contentId
        this.creatorId = creatorId
        this.currentUserId = currentUserId
        if (afterLoad) {
            mViewModel.setData(contentId, creatorId)
        }
    }

    fun setNewComment(comment: CommentRecordBean){
        if (isViewModelInit()) {
            mViewModel.setNewComment(comment)
        }
    }

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        bindAction(mViewModel.action_finishRefresh){
            mBinding.refreshLayout.finishRefresh()
        }
        bindAction(mViewModel.action_finishLoadMore){
            mBinding.refreshLayout.finishLoadMore()
        }
        bindAction(mViewModel.action_networkError){
            mBinding.multiStateContainer.show<NetworkErrorState> {
                it.onRetry {
                    mViewModel.onErrorRetry()
                }
            }
        }
        bindData(mViewModel.data_commentList){
            mBinding.multiStateContainer.show<SuccessState>()
            mAdapter.setIds(creatorId, currentUserId)
            mAdapter.list = it
            mAdapter.notifyDataSetChanged()
        }
        bindData(mViewModel.data_enableLoadMore){
            mBinding.refreshLayout.setEnableLoadMore(it)
        }
    }

    override fun initView() {
        mAdapter = CommentContentAdapter(arrayListOf(), mListener)
        mBinding.recyclerView.adapter = mAdapter

        val linearLayoutManager = LinearLayoutManager(context)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.getCommentList()
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            //OnLoadMore
            mViewModel.onLoadMoreCommentList()
        }
        //
        afterInit = true
    }

    override fun singleLazyLoad() {
        mViewModel.setData(contentId, creatorId)
        //
        afterLoad = true
    }

    override fun onResume() {
        super.onResume()
        refreshPage()
    }

    fun refreshPage(){
        //
        mAddCommentDialog?.dismiss()
        mDeletePopupWindow?.dismiss()
        //
        val state = mBinding.multiStateContainer.currentState
        val isSuccess = state == null || state is SuccessState
        if (isSuccess) {
            mBinding.refreshLayout.autoRefresh()
            return
        }
        if(state is NetworkErrorState){
            mViewModel.onErrorRetry()
        }
    }

    private val mListener = object : CommentContentAdapter.CommentListener {

        override fun onReplyComment(parent: CommentRecordBean?, data: CommentRecordBean) {
            context?.let { ctx ->
                mAddCommentDialog = AddCommentDialog(ctx)
                mAddCommentDialog!!.autoShow(data.channelName) {
                    mViewModel.onReplyComment(parent, data, it)
                }
            }
        }

        override fun onBtnMoreClick(anchor: View, parent: CommentRecordBean?, data: CommentRecordBean) {
            //
            val location = IntArray(2)
            anchor.getLocationInWindow(location)
            val locationY = location[1]
            val rootheight = anchor.rootView.height
            val isUp: Boolean = locationY > rootheight * 0.6
            //
            mDeletePopupWindow = DeletePopupWindow.autoShow(anchor.context, anchor, isUp){
                mViewModel.onDeleteComment(parent, data)
            }
        }

        override fun onViewMoreComment(data: CommentRecordBean) {
            mViewModel.onViewMoreComment(data)
        }

        override fun onHideComment(data: CommentRecordBean) {
            mViewModel.onHideComment(data)
        }
    }
}